<?php require_once '../global.php';

function king_def(){
	global $king;
	$king->access('keywords');
	$king->access("search");


	$nfc=kc_get('nfc',0);
	$query=kc_get('words',0); 

	$querys=preg_split("/[,\*\%\.\(\)\'\`><\}\{ ]/",$query);

	$querys=array_diff($querys,array(''));












	if($nfc=='1'){

	$q=implode("%' or ktitle like '%",$querys);
	if(isset($q{0})){
		$q=" and (ktitle like '%".$q."%')";
	}

			$_cmd=array(
					'delete'=>$king->lang->get('system/common/del'),
				);
				$manage="'<a href=\"manage.php?action=view&kid='+K[0]+'\">'+\$.kc_icon('q7','".addslashes($king->lang->get('system/common/view'))."')+'</a>'";
				$manage.="+'<a href=\"javascript:;\" class=\"k_ajax\" rel=\"{CMD:\'delete\',list:'+K[0]+'}\">'+\$.kc_icon('q8','".addslashes($king->lang->get('system/common/del'))."')+'</a>'";
				$manage.="+\$.kc_updown(K[0])";

				$_js=array(
					"\$.kc_list(K[0],K[1],'manage.php?action=view&kid='+K[0])",
					$manage,
					"'<i>'+ishow('manage.php',K[0],K[2])+'</i>'",//显示
					"K[3]",
					"K[4]",
				);
					
				$s=$king->openList($_cmd,'',$_js,$king->db->pagelist('search.php?words='.$query.'&pid=PID&rn=RN',$king->db->getRows_number('%s_key',"kid!=0 ".$q)));











		$_sql="select kid,ktitle,ktag,nshow,ndate from %s_key where kid!=0 ".$q." order by norder desc,kid desc";
		if(!$res=$king->db->getRows($_sql,1)) $res=array();


			$s.="function ishow(url,id,is){var I1,ico;is?ico='n1':ico='n2';";
			$s.="I1='<a id=\"nshow_'+id+'\" class=\"k_ajax\" rel=\"{CMD:\'show\',value:'+ (1-is) +',ID:\'nshow_'+id+'\',list:'+id+',IS:2}\" >'+$.kc_icon(ico,(is?'".$king->lang->get('keywords/list/unshow')."':'".$king->lang->get('keywords/list/show')."'))+'</a>';return I1;};";
			$s.="ll('".$king->lang->get('keywords/list/title')."',
				'manage',
				'".$king->lang->get('keywords/list/show')."',
				'".$king->lang->get('keywords/list/ktag')."',
				'".$king->lang->get('keywords/list/date')."',1);";


			foreach($res as $rs){
				$s.="ll({$rs['kid']},
					'".addslashes(htmlspecialchars($rs['ktitle']))."',
					".$rs['nshow'].",
					'".$rs['ktag']."',
					'".kc_formatdate($rs['ndate'])."',0);";
			}



	}elseif($nfc=='2'){


			$q=implode("%' or name like '%",$querys);
			if(isset($q{0})){
				$q=" and (name like '%".$q."%')";
			}

			$_cmd=array(
				'delete'=>$king->lang->get('system/common/del'),
			);
			$manage ="'<a target=\"_blank\" href=\"/tag/'+(K[3])+'".$king->config('rewriteend')."\">'+$.kc_icon('h7','".$king->lang->get('system/common/brow')."')+'</a>'";
			$manage.="+'<a href=\"javascript:;\" class=\"k_ajax\" rel=\"{CMD:\'sjdelete\',list:'+K[0]+'}\">'+\$.kc_icon('l5','".addslashes($king->lang->get('system/common/del'))."')+'</a>'";
			$manage.="+'<a href=\"../soso/search.php?ktitle='+(K[1])+'\">'+\$.kc_icon('h6','".addslashes('添加知道')."')+'</a>'";
			$manage.="+\$.kc_updown(K[0])";

			$_js=array(
				"\$.kc_list(K[0],K[1],'manage_sj.php?action=view&mid='+K[0])",
				$manage,
				"'<i>'+ishow('manage_sj.php',K[0],K[2])+'</i>'",//显示
				"K[3]",
				"K[4]",
			);
			$s=$king->openList($_cmd,'',$_js,$king->db->pagelist('manage_sj.php?pid=PID&rn=RN',$king->db->getRows_number('metas',"mid!=0 ' ".$q)));




			$_sql="select mid,name,slug,nshow,count from metas where mid!=0 ".$q." order by nshow asc,mid desc";
			if(!$res=$king->db->getRows($_sql,1)) $res=array();
	

			$s.="function ishow(url,id,is){var I1,ico;is?ico='n1':ico='n2';";
			$s.="I1='<a id=\"nshow_'+id+'\" class=\"k_ajax\" rel=\"{CMD:\'sj_show\',value:'+ (1-is) +',ID:\'nshow_'+id+'\',list:'+id+',IS:2}\" >'+$.kc_icon(ico,(is?'".$king->lang->get('keywords/list/unshow')."':'".$king->lang->get('keywords/list/show')."'))+'</a>';return I1;};";
			$s.="ll('".$king->lang->get('keywords/list/title')."',
				'manage',
				'".$king->lang->get('keywords/list/show')."',
				'".$king->lang->get('keywords/list/ktag')."',
				'".$king->lang->get('keywords/list/date')."',1);";


			foreach($res as $rs){
				$s.="ll({$rs['mid']},
					'".addslashes(htmlspecialchars($rs['name']))."',
					".$rs['nshow'].",
					'".$rs['slug']."',
					'".$rs['count']."',0);";
			}


	
	}else{



	$q=implode("%' or ktag like '%",$querys);
	if(isset($q{0})){
		$q=" and (ktag like '%".$q."%')";
	}



		$_cmd=array(
				'tagdelete'=>$king->lang->get('system/common/del'),
			);
			$brow_link=($king->config('rewritetag') ? "../tag" : "../index.php/tag").$king->config('rewriteline');
	$manage="'<a href=\"manage.php?action=view&kid='+K[0]+'\">'+\$.kc_icon('q7','".addslashes($king->lang->get('system/common/view'))."')+'</a>'";
	$manage.="+'<a href=\"javascript:;\" class=\"k_ajax\" rel=\"{CMD:\'tagdelete\',list:'+K[0]+'}\">'+\$.kc_icon('q8','".addslashes($king->lang->get('system/common/del'))."')+'</a>'";
	$manage.="+\$.kc_updown(K[0])";

	$manage ="'<a target=\"_blank\" href=\"{$brow_link}'+(K[1])+'".$king->config('rewriteend')."\">'+$.kc_icon('h7','".$king->lang->get('system/common/brow')."')+'</a>'";
	$manage.="+'<a href=\"../portal/manage.tag.php?action=edt&kid='+K[0]+'\">'+$.kc_icon('k5','".$king->lang->get('system/common/edit')."')+'</a>'";
	$manage.="+'<a class=\"k_ajax\" rel=\"{CMD:\'tagdelete\',list:'+K[0]+'}\">'+$.kc_icon('l5','".$king->lang->get('system/common/del')."')+'</a>'";


	$_js=array(
		"$.kc_list(K[0],K[1],'../portal/manage.tag.php?action=edt&kid='+K[0])",
		$manage,
		"'<i>'+ishow('manage.php',K[0],K[2])+'</i>'",//显示
		"K[3]",

	);
	$s=$king->openList($_cmd,'',$_js,$king->db->pagelist('manage_tag.php?pid=PID&rn=RN'.$url,$king->db->getRows_number('%s_tag','kid!=0'.$q)));

	$_sql="select kid,ktag,nshow,kcolor,nsize,isbold,iscommend,ndate from %s_tag where kid!=0 ".$q." order by nshow asc,kid desc";
		
				if(!$res=$king->db->getRows($_sql,1)) $res=array();

$s.="function ishow(url,id,is){var I1,ico;is?ico='n1':ico='n2';";
	$s.="I1='<a id=\"nshow_'+id+'\" class=\"k_ajax\" rel=\"{CMD:\'tag_show\',value:'+ (1-is) +',ID:\'nshow_'+id+'\',list:'+id+',IS:2}\" >'+$.kc_icon(ico,(is?'".$king->lang->get('keywords/list/unshow')."':'".$king->lang->get('keywords/list/show')."'))+'</a>';return I1;};";
	
	$s.="ll('".$king->lang->get('keywords/list/ktag')."',
		'".$king->lang->get('keywords/list/show')."',
		'".$king->lang->get('keywords/list/show')."',
	    '".$king->lang->get('keywords/list/date')."',1);";


	foreach($res as $rs){
		$s.="ll({$rs['kid']},
		    '".addslashes(htmlspecialchars($rs['ktag']))."',
			 ".$rs['nshow'].",
		    '".kc_formatdate($rs['ndate'])."',0);";
	}












	}
	










	$s.=$king->closeList();

	list($left,$right)=inc_menu();

			
	$king->skin->output($king->lang->get('keywords/title/center'),$left, $bs,$s);

}


/**
菜单调用
*/
function inc_menu(){
	global $king;
	$left=array(
		'0'=>array(
			'href'=>'manage.php',
			'ico'=>'p7',
			'title'=>$king->lang->get('keywords/list/title'),
			),
		'1'=>array(
			'href'=>'manage_tag.php',
			'ico'=>'p7',
			'title'=>$king->lang->get('keywords/list/ktag'),
			),
		'fc'=>array(
			'href'=>'javascript:;',
			'ico'=>'c2',
			'title'=>'添加分词',
			'rel'=>"{CMD:'fc',METHOD:'GET',URL:'./index.php'}",
			'class'=>"k_ajax",
		),
		'edt'=>array(
			'href'=>'javascript:;',
			'ico'=>'c2',
			'title'=>'添加关键词',
			'rel'=>"{CMD:'edt',METHOD:'GET',URL:'./index.php'}",
			'class'=>"k_ajax",
		),
	);
	

	return array($left,array());
}



//删除留言
function king_ajax_delete(){
	global $king;
	$king->access('keywords_delete');
	$list=kc_getlist();
	$king->db->query("delete from %s_key where kid in ($list)");
	kc_ajax('OK',"<p class=\"k_ok\">".$king->lang->get('system/ok/delete')."</p>",1);
}

//删除留言
function king_ajax_tagdelete(){
	global $king;
	$king->access('keywords_delete');
	$list=kc_getlist();
	$king->db->query("delete from %s_tag where kid in ($list)");
	kc_ajax('OK',"<p class=\"k_ok\">".$king->lang->get('system/ok/delete')."</p>",1);
}

//删除留言
function king_ajax_sjdelete(){
	global $king;
	$king->access('keywords_delete');
	$list=kc_getlist();
	$king->db->query("delete from metas where mid in ($list)");
	kc_ajax('OK',"<p class=\"k_ok\">".$king->lang->get('system/ok/delete')."</p>",1);
}
//排序
function king_ajax_updown(){
	global $king;
	$king->access('key_updown');

	$kid=kc_get('kid',2,1);
	$king->db->updown('%s_key',$kid);
}

//显示隐藏
function king_ajax_show(){
	global $king;
	$king->access('keywords_edt');
	$kid=kc_get('list',2,1);
	$value=kc_get('value',2,1);
	$king->db->query("update %s_key set nshow=$value where kid=$kid;");
	$value ? $ico='n1':$ico='n2';
	kc_ajax('',
		kc_icon($ico,($value?$king->lang->get('keywords/list/unshow'):$king->lang->get('keywords/list/show'))),
		0,
		"$('#nshow_{$kid}').attr('rel','{CMD:\'show\',value:".(1-$value).",ID:\'nshow_{$kid}\',list:\'$kid\',IS:2}')");
}

//显示隐藏
function king_ajax_tag_show(){
	global $king;
	$king->access('keywords_edt');
	$kid=kc_get('list',2,1);
	$value=kc_get('value',2,1);
	$king->db->query("update %s_tag set nshow=$value where kid=$kid;");



	if($value=='1'){
						$keyid=$king->db->getRows_one("select ktag from %s_tag where kid='".$kid."'");
						$king->db->query("delete from %s_key_tag_id where tagid ='".$kid."'");
						$king->db->query("delete from %s_key where ktitle like '%".$keyid['ktag']."%'");	
	}


	$value ? $ico='n1':$ico='n2';
	kc_ajax('',
		kc_icon($ico,($value?$king->lang->get('keywords/list/unshow'):$king->lang->get('keywords/list/show'))),
		0,
		"$('#nshow_{$kid}').attr('rel','{CMD:\'show\',value:".(1-$value).",ID:\'nshow_{$kid}\',list:\'$kid\',IS:2}')");
}
//显示隐藏
function king_ajax_sj_show(){
	global $king;
	$king->access('keywords_edt');
	$mid=kc_get('list',2,1);
	$value=kc_get('value',2,1);
	$king->db->query("update metas set nshow=$value where mid=$mid;");
	$value ? $ico='n1':$ico='n2';
	kc_ajax('',
		kc_icon($ico,($value?$king->lang->get('keywords/list/unshow'):$king->lang->get('keywords/list/show'))),
		0,
		"$('#nshow_{$mid}').attr('rel','{CMD:\'show\',value:".(1-$value).",ID:\'nshow_{$mid}\',list:\'$mid\',IS:2}')");
}



function king_view(){
	global $king;
	$king->access('keywords');
	
	$kid=kc_get('kid',2);
	$sql="*";

	if(!$res=$king->db->getRows("select $sql from %s_key where kid=$kid"))
		$res=array();

	if(empty($kid)){
		kc_error($king->lang->get('system/error/param'));
	}else{
		if(!$rs=$king->db->getRows_one("select $sql from %s_key where kid=$kid order by kid asc,norder asc"))
			kc_error($king->lang->get('system/error/notrecord'));

		foreach ($rs as &$r) {
			$r=htmlspecialchars($r);
		}
		$rs['kcontent']=nl2br($rs['kcontent']);

		$s=$king->openForm($king->lang->get('keywords/name'),'','keywords_edt');
		$s.=$king->htmForm($king->lang->get('keywords/label/title'),$rs['ktitle']);
		$s.=$king->htmForm($king->lang->get('keywords/label/name'),$rs['ktag']);
		$s.=$king->htmForm($king->lang->get('keywords/label/content'),$rs['kcontent']);
		$s.=$king->htmForm($king->lang->get('keywords/label/date'),kc_formatdate($rs['ndate']));

		$but='<input type="button" onclick="javascript:history.back(-1)" value="'.$king->lang->get('system/common/back').'[B]" class="big" accesskey="b"/>';

		$s.=$king->htmForm(null,$but);
		$s.=$king->closeForm('none');
	}




	list($left,$right)=inc_menu();
	$king->skin->output($king->lang->get('keywords/title/center'),$left,$right,$s);

}
?>